package com.example.boke.controller;

import com.example.boke.entity.Comment;
import com.example.boke.entity.User;
import com.example.boke.mapper.CommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @author Xdoor
 * @version 1.0
 * @date 2021/10/20 17:43
 */

@RestController
public class CommentController
{
    @Autowired(required = false)
    CommentMapper mapper;

    @RequestMapping("/comment")
    public int comment(Comment comment, HttpSession session)
    {
        User user = (User) session.getAttribute("user");
        System.out.println(user);
        if (user == null)
        {
            return 2;//未登录
        }

        //comment里面没有作者,时间,bokeId
        comment.setAuthor(user.getNick());
        comment.setCreated(new Date());

        mapper.insert(comment);

        return 1;//成功
    }

    @RequestMapping("/comment/selectBybokeId")
    public List<Comment> selectBybokeId(int id)
    {
        return mapper.selectBybokeId(id);
    }

    @RequestMapping("/comment/user/delete")
    public void delete(int id, HttpSession session)
    {
        User user = (User) session.getAttribute("user");
        Integer userId = user.getId();
        mapper.delete(id, userId);
    }
}
